package com.sinosoft.mcp.client.mysql;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.mcp.spring.McpFunctionCallback;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;

import java.util.List;

@SpringBootApplication
public class MysqlMcpClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(MysqlMcpClientApplication.class, args);
    }

    @Bean
    public CommandLineRunner predefinedQuestions(ChatClient.Builder chatClientBuilder,
                                                 List<McpFunctionCallback> functionCallbacks, ConfigurableApplicationContext context) {

        return args -> {
            var chatClient = chatClientBuilder
                    .defaultFunctions(functionCallbacks.toArray(new McpFunctionCallback[0]))
                    .build();
            System.out.println("运行带有AI模型回答的预定义问题：\n");

            // Question 1
            String question1 = "你能连接到我的mysql数据库并告诉我有哪些表吗？";
            System.out.println("QUESTION: " + question1);
            System.out.println("ASSISTANT: " + chatClient.prompt(question1).call().content());

            // Question 2
            String question2 = "数据库中所有产品的平均价格是多少？";
            System.out.println("\nQUESTION: " + question2);
            System.out.println("ASSISTANT: " + chatClient.prompt(question2).call().content());

            System.out.println("\n预先设定的问题已完成。退出应用程序。");
            context.close();

        };
    }

}
